package com.qianchang.microfinance.view.mine.loan;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.FindsBean;
import com.qianchang.microfinance.view.findings.FindsAdapter;
import java.util.ArrayList;
import java.util.List;

public class RecommendAdapter extends RecyclerView.Adapter<RecommendAdapter.GridViewHolder> implements View.OnClickListener{

    private Context mContext;
    //泛型是RecyclerView所需的Bean类
    private List<String> mDateBeen;
    private List<FindsBean> listFindBean = new ArrayList<FindsBean>();
    private FindsAdapter.OnItemClickListener mOnItemClickListener = null;

    public RecommendAdapter(Context context, List<FindsBean> dateBeen) {
        mContext = context;
        listFindBean = dateBeen;
    }

    public List<FindsBean> getItems(){
        return listFindBean;
    }


    @Override
    public GridViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = View.inflate(mContext, R.layout.item_recommendgrid_layout, null);
        itemView.setOnClickListener(this);
        //创建一个ViewHodler对象
        GridViewHolder gridViewHolder = new GridViewHolder(itemView);
        //把ViewHolder传出去
        return gridViewHolder;
    }

    @Override
    public void onBindViewHolder(GridViewHolder holder, int position) {
        GridViewHolder gridViewHolder = (GridViewHolder)holder;
        gridViewHolder.itemView.setTag(position);
        FindsBean findsBean = listFindBean.get(position);

        if(position != 5){
            Glide.with(mContext).load(findsBean.getImageUrl()).error(R.drawable.default_img).into(gridViewHolder.mImageView);
            gridViewHolder.mTextView.setText(findsBean.getName());
        }else {
            gridViewHolder.mTextView.setText("更多");
            gridViewHolder.mImageView.setImageResource(R.drawable.more);
        }

    }

    @Override
    public int getItemCount() {
        if (listFindBean != null && listFindBean.size() > 0) {
            if(listFindBean.size() >= 6){
                return 6;
            }else {
                return listFindBean.size();
            }
        }
        return 0;
    }

    public void setData(List<FindsBean> mlistFindBean){
        if(mlistFindBean != null){
            listFindBean = mlistFindBean;
        }
        notifyDataSetChanged();
    }


    @Override
    public void onClick(View view) {
        if(mOnItemClickListener != null){
            mOnItemClickListener.onItemClick(view,(int)view.getTag());
        }
    }

    public class GridViewHolder extends RecyclerView.ViewHolder {
        private final ImageView mImageView;
        private final TextView mTextView;

        public GridViewHolder(View itemView) {
            super(itemView);
            mImageView = (ImageView) itemView.findViewById(R.id.item_list_icon);
            mTextView = (TextView) itemView.findViewById(R.id.item_list_name);
        }
    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(FindsAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

}
